package com.webbdong.aop.agent.test.bytebuddy;

import net.bytebuddy.description.modifier.ModifierContributor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2023-03-31 10:25
 **/
public class BytebuddyVisit2 {

    private Class<?> adviceInterceptor;

    private List<ClassDefinition> classMethods;

    private List<DefineField> defineFields;

    public List<String> getClasses() {
        if (classMethods == null || classMethods.isEmpty()) {
            return null;
        }
        return classMethods.stream().map(ClassDefinition::getName).collect(Collectors.toList());
    }

    public List<String> getMethods() {
        if (classMethods == null || classMethods.isEmpty()) {
            return null;
        }
        List<List<MethodDefinition>> collect = classMethods.stream().map(e -> e.getMethods()).collect(Collectors.toList());
        List<String> list = new ArrayList<>();
        collect.forEach(e->{
            list.addAll(e.stream().map(MethodDefinition::getName).collect(Collectors.toList()));
        });
        return list;
    }

    public Class<?> getAdviceInterceptor() {
        return adviceInterceptor;
    }

    public void setAdviceInterceptor(Class<?> adviceInterceptor) {
        this.adviceInterceptor = adviceInterceptor;
    }

    public List<ClassDefinition> getClassMethods() {
        return classMethods;
    }

    public void setClassMethods(List<ClassDefinition> classMethods) {
        this.classMethods = classMethods;
    }

    public List<DefineField> getDefineFields() {
        return defineFields;
    }

    public void setDefineFields(List<DefineField> defineFields) {
        this.defineFields = defineFields;
    }

    public static class DefineField {
        private String name;
        private Class<?> type;

        private List<ModifierContributor.ForField> forFields;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Class<?> getType() {
            return type;
        }

        public void setType(Class<?> type) {
            this.type = type;
        }

        public List<ModifierContributor.ForField> getForFields() {
            return forFields;
        }

        public void setForFields(List<ModifierContributor.ForField> forFields) {
            this.forFields = forFields;
        }
    }

    public static class ClassDefinition {
        private String name;
        private List<MethodDefinition> methods;
        private boolean allMethods = false;

        public ClassDefinition() {
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<MethodDefinition> getMethods() {
            return methods;
        }

        public void setMethods(List<MethodDefinition> methods) {
            this.methods = methods;
        }

        public boolean isAllMethods() {
            return allMethods;
        }

        public void setAllMethods(boolean allMethods) {
            this.allMethods = allMethods;
        }
    }

    public static class MethodDefinition {

        private String name;

        private List<String> paramTypes;

        private boolean allMethods;

        public MethodDefinition() {
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<String> getParamTypes() {
            return paramTypes;
        }

        public void setParamTypes(List<String> paramTypes) {
            this.paramTypes = paramTypes;
        }

        public boolean isAllMethods() {
            return allMethods;
        }

        public void setAllMethods(boolean allMethods) {
            this.allMethods = allMethods;
        }
    }

}
